﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BskLeague.Models;
using System.Net;

namespace BskLeague.Controllers
{
    public class StadiumController : Controller
    {
        BskLeagueDB _db = new BskLeagueDB();

        public ActionResult Index()
        {
            var stadiums = (from m in _db.Stadiums
                            where m.stadiumActive == true
                            select m).ToList();

            return View(stadiums);
        }

        public ActionResult Details(int id)
        {
            var stadium = _db.Stadiums.Find(id);
            return View(stadium);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Edit(int id)
        {
            var stadium = _db.Stadiums.Find(id);
            return View(stadium);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Delete(int id)
        {
            var stadium = _db.Stadiums.Find(id);
            stadium.stadiumActive = false;

            _db.Entry(stadium).State = System.Data.EntityState.Modified;
            _db.SaveChanges();

            return RedirectToAction("Index");
        }

        [Authorize(Roles = "Admin")]
        public ActionResult New()
        {
            return View();
        }

        [Authorize(Roles = "Admin")]
        [HttpPost]
        public ActionResult Create(string stadiumName, string stadiumAddressStreet, string stadiumAddressCity, string stadiumPhotoUrl)
        {
            Stadium stadium = new Stadium();

            stadium.stadiumName = stadiumName;
            stadium.stadiumAddressStreet = stadiumAddressStreet;
            stadium.stadiumAddressCity = stadiumAddressCity;
            stadium.stadiumPhotoUrl = stadiumPhotoUrl;
            stadium.stadiumActive = true;

            var client = new WebClient();
            string region = stadiumAddressStreet + " " + stadiumAddressCity;
            string uri = "http://maps.google.com/maps/geo?q=" + region + "&output=csv";
            string[] geocodeInfo = client.DownloadString(uri).Split(',');

            string posY = geocodeInfo[2].Replace(".", ",");
            string posX = geocodeInfo[3].Replace(".", ",");

            stadium.stadiumPosY = Convert.ToDouble(posY);
            stadium.stadiumPosX = Convert.ToDouble(posX);

            _db.Stadiums.Add(stadium);
            _db.SaveChanges();

            return RedirectToAction("Index");
        }

        [Authorize(Roles = "Admin")]
        [HttpPost]
        public ActionResult Update(int stadiumId, string stadiumName, string stadiumAddressStreet, string stadiumAddressCity, string stadiumPhotoUrl)
        {
            Stadium stadium = _db.Stadiums.Find(stadiumId);

            stadium.stadiumName = stadiumName;
            stadium.stadiumAddressStreet = stadiumAddressStreet;
            stadium.stadiumAddressCity = stadiumAddressCity;
            stadium.stadiumPhotoUrl = stadiumPhotoUrl;

            var client = new WebClient();
            string region = stadiumAddressStreet + " " + stadiumAddressCity;
            string uri = "http://maps.google.com/maps/geo?q=" + region + "&output=csv";
            string[] geocodeInfo = client.DownloadString(uri).Split(',');

            string posY = geocodeInfo[2].Replace(".", ",");
            string posX = geocodeInfo[3].Replace(".", ",");

            stadium.stadiumPosY = Convert.ToDouble(posY);
            stadium.stadiumPosX = Convert.ToDouble(posX);

            _db.Entry(stadium).State = System.Data.EntityState.Modified;
            _db.SaveChanges();

            return RedirectToAction("Details", new { id = stadiumId });
        }
    }
}
